package com.wqz.day.day.up.config;

import com.wqz.day.day.up.queue.WebDriverQueue;
import lombok.extern.slf4j.Slf4j;
import org.openqa.selenium.chrome.ChromeOptions;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * This is a class template with author information.
 *
 * @author wuqz
 * @date 2024/3/7
 */
@Configuration
@Slf4j
public class WebDriverConfigure {


    /**
     * chromeDriver驱动地址
     */
    @Value("${selenium.chrome.driver.path}")
    private String chromeDriverPath;

    /**
     * webDriverQueue的初始化对象的数量
     */
    @Value("${seleninum.driver.instance.num}")
    private Integer seleninumDriverInstanceNum;




    @Bean(value = "webDriverQueue", destroyMethod = "destory")
    WebDriverQueue webDriverQueue() {


        // 创建ChromeOptions对象
        ChromeOptions options = new ChromeOptions();
        // 添加无头模式参数
        options.addArguments("--headless");
        // 如果需要的话，还可以添加其他参数，如视口大小
        //options.addArguments("--window-size=1920, 1146");

        //禁用沙箱功能
        options.addArguments("--no-sandbox");

        //禁用使用 /dev/shm
        options.addArguments("--disable-dev-shm-usage");

        //禁用崩溃报告功能
        options.addArguments("--disable-crash-reporter");

        //静音模式
        options.addArguments("--silent");

        //禁用所有浏览器扩展
        options.addArguments("--disable-extensions");

        WebDriverQueue webDriverQueue = new WebDriverQueue("WebDriverQueue", 0, options);

        try {
            // 设置ChromeDriver的路径
            System.setProperty("webdriver.chrome.driver", chromeDriverPath);
            System.setProperty("webdriver.chrome.whitelistedIps", "");

            // 初始化WebDriver
            webDriverQueue = new WebDriverQueue("WebDriverQueue", seleninumDriverInstanceNum, options);

            return webDriverQueue;
        } catch (Exception e) {
            webDriverQueue.destory();
            log.error("初始化WebDriverQueue异常！", e);

        } finally {
            return webDriverQueue;
        }
    }

}
